package com.whty.cms.filewps.upload.controller;

import com.whty.cms.filewps.common.message.ResponseMessage;
import com.whty.cms.filewps.upload.dto.TeamworkEdit;
import com.whty.cms.filewps.upload.service.IEditService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = {"/teamworkEdit"})
public class TeamworkEditController {

    private static final Logger logger = LogManager.getLogger(TeamworkEditController.class);

    @Autowired
    private IEditService editService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage save(@RequestBody TeamworkEdit teamworkEdit) {
        ResponseMessage result = new ResponseMessage();
        try {
            if(StringUtils.isBlank(teamworkEdit.getFid())||StringUtils.isBlank(teamworkEdit.getUserId()) ||StringUtils.isBlank(teamworkEdit.getType())) {
                return returnMessage(result, "100000","必填参数为空","" );
            }
            List<TeamworkEdit>  list = editService.selectRes(teamworkEdit);
            if (list != null && !list.isEmpty()) {
                editService.updateByPrimaryKey(teamworkEdit);
            } else {
                editService.save(teamworkEdit);
            }
            return returnMessage(result, "000000","保存成功","" );
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return returnMessage(result, "400000","系统内部错误",e.getMessage() );
        }
    }

    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage selectAll(@RequestBody TeamworkEdit teamworkEdit) {
        ResponseMessage result = new ResponseMessage();
        try {
            if(StringUtils.isBlank(teamworkEdit.getFid())||StringUtils.isBlank(teamworkEdit.getUserId())) {
                return returnMessage(result, "100000","必填参数为空","" );
            }
            List<TeamworkEdit> list  = editService.selectAll(teamworkEdit);
            return returnMessage(result,"000000","成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return returnMessage(result, "400000","系统内部错误",e.getMessage() );
        }
    }


    private ResponseMessage returnMessage(ResponseMessage result, String code, String msg, Object data) {
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
